#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
using namespace std;

#define MAX_COUNT 128

vector<int> split2word( const string& str, map<string, int>& dict, int& count )
{
	vector<int> ids;

	map<string, int>::iterator it;
	istringstream ss( str );
	string token;
	while( ss >> token )
	{
		//cout << "DICT: " << token << endl;
		it = dict.find( token );
		if( it != dict.end() )
		{
			ids.push_back( it->second );
		}
		else
		{
			dict[token] = count;
			ids.push_back( count );
			count++;
		}
	}
	return ids;
}

struct LCSN
{
	int length;
	vector<int> records;

	LCSN()
	{
		length = 0;
	}
};

LCSN LCS( int* seq1, int* seq2, int len1, int len2 )
{
	LCSN F[ MAX_COUNT ];

	for( int i = 1; i <= len2; i++ )
	{
		LCSN t = F[0];
		for( int j = 1; j <= len1; j++ )
		{
			LCSN q = F[j];
			if( seq2[i-1] == seq1[j-1] )
			{
				F[j].length = t.length + 1;
				F[j].records = t.records;
				F[j].records.push_back( seq1[j-1] );
			}
			else
			{
				if( F[j].length < F[j-1].length )
				{
					F[j] = F[j-1];
				}
			}
			t = q;
		}
	}

	return F[ len1 ];
}

int main()
{
	while( true )
	{
		int count = 1;
		map<string, int> dict;
		string s;
		if( !getline( cin, s, '#' ) )
		{
			break;
		}
		vector<int> words1 = split2word( s, dict, count );
		if( !getline( cin, s, '#' ) )
		{
			break;
		}
		vector<int> words2 = split2word( s, dict, count );

		map<int, string> dict2;
		for( map<string, int>::iterator it = dict.begin(); it != dict.end(); it++ )
		{
			dict2[it->second] = it->first;
		}

		//LIS
		LCSN ret = LCS( &words1[0], &words2[0], words1.size(), words2.size() );

		bool first = true;
		for( vector<int>::iterator it = ret.records.begin(); it != ret.records.end(); it++ )
		{
			if( !first ) cout << " ";
			first = false;
			cout << dict2[*it];
		}
		cout << endl;

	}
	return 0;
}
